using UnityEngine;
using System.Collections;

/// <summary>
/// 处理确认按钮点击的组件
/// </summary>
public class ConfirmButtonClickHandler : MonoBehaviour
{
    private RewardPageUI rewardPageUI;
    private bool _canBeSelected = true;

    public void Initialize(RewardPageUI ui)
    {
        rewardPageUI = ui;
    }

    /// <summary>
    /// 鼠标点击事件
    /// </summary>
    private void OnMouseDown()
    {
        if (!_canBeSelected) return;

        Debug.Log("===点击确认按钮===");

        if (rewardPageUI != null)
        {
            rewardPageUI.OnConfirmButtonClicked();
        }

        if (!gameObject.activeInHierarchy)
        {
            Debug.Log("确认按钮处于非激活状态，取消播放动画");
            return;
        }

        // 播放点击动画
        StartCoroutine(ClickAnimation());
    }


    /// <summary>
    /// 点击动画
    /// </summary>
    private IEnumerator ClickAnimation()
    {
        Vector3 originalScale = transform.localScale;
        transform.localScale = originalScale * 0.9f;

        yield return new WaitForSeconds(0.1f);

        transform.localScale = originalScale;
    }

    /// <summary>
    /// 设置按钮是否可点击
    /// </summary>
    public void SetInteractable(bool interactable)
    {
        _canBeSelected = interactable;

        // 改变外观表示状态
        SpriteRenderer renderer = GetComponent<SpriteRenderer>();
        if (renderer != null)
        {
            renderer.color = interactable ? Color.white : Color.gray;
        }
    }

    /// <summary>
    /// 检查是否可以直接点击（战斗失败且没有奖励卡牌）
    /// </summary>
    public bool CanClickDirectly()
    {
        if (rewardPageUI == null) return false;

        // 使用反射或其他方式获取RewardPageUI的私有字段
        // 这里需要在RewardPageUI中添加公共方法来检查这些条件
        return rewardPageUI.CanConfirmButtonClickDirectly();
    }
}